# -*- coding: utf-8 -*-
# Create your views here.
from cmsdj.content.models import Content
#from cmsdj.categories.models import Category
from cmsdj.cfg.models import SiteCfg
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.template.context import RequestContext
from cmsdj.settings import TEMPLATE_DIRS
from cmsdj.util.keywords import get_keywords_text

def index (request, page=1):
    
    if not hasattr(request, 'current_cfg'): 
        request.current_cfg=SiteCfg.current_cfg()
        template_dir = SiteCfg.get_template_dir()
        if (not template_dir in TEMPLATE_DIRS):
            TEMPLATE_DIRS.append(template_dir)    
    
    contents = Content.get_by_date_created()
    
    keywords = ""
    for content in contents:
        keywords = keywords + content.get_keywords()
#        for category in content.categories.all:
#            keywords = keywords + category.get_keywords()
    
    keywords = get_keywords_text( keywords , 10 ) 

    keywords = get_keywords_text( keywords + "," + request.current_cfg.get_keywords() , 10 ) 
    
    paginator = Paginator(contents, request.current_cfg.items_per_page)

    try:
        page_contents = paginator.page(page)
    except (EmptyPage, InvalidPage):
        page_contents = paginator.page(paginator.num_pages)

    return render_to_response ( 'base.html', locals(), context_instance=RequestContext(request))
